package org.xt.module.admin;

import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.xt.constants.Const;
import org.xt.constants.Res;
import org.xt.entity.Admin;

/**
 * 管理员module
 * @author XiangTao
 *
 */
@IocBean
@At("/admin")
public class AdminModule {
	@Inject
	private Dao dao;
	
	@At("/login")
	@GET
	@Ok("jsp:admin.login")
	@Filters()
	public void login(){
		
	}
	
	@At({"/index", "/"})
	@GET
	@Ok("jsp:admin.index")
	public void index(){
		
	}
	
	@At("/login")
	@POST
	@Ok("json")
	@Filters
	public Res login(@Param("..")Admin admin, HttpSession session){
		System.out.println(dao);
		System.out.println(admin);
		admin = dao.fetch(Admin.class, Cnd.where("username", "=", admin.getUsername()).and("password", "=", admin.getPassword()));
		if(admin != null){
			session.setAttribute(Const.LOGIN_ADMIN_SIGN, admin);
			return Res.NEW().code(Res.SUCCESS).msg("管理员登录成功").data(admin);
		}else{
			return Res.NEW().code(Res.ERROR).msg("管理员登录失败");
		}
		
	}
}
